package com.tensquare.gathering.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.gathering.dao.Impl.GatheringDao;
import com.tensquare.gathering.pojo.Gathering;
import entity.PageResult;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;

/**
 * @Description
 * @Date 2021/3/21 19:50
 */
@Component
@Aspect
public class GatheringService {
    @Autowired
    private GatheringDao gatheringDao;

    @Autowired
    private IdWorker idWorker;


    @Pointcut("within(com.tensquare.gathering.service..*)")
    public void GatheringService(){

    }

    @Around("GatheringService()") // 环绕通知
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();// 记录开始时间
        Object result = joinPoint.proceed();
        System.out.println(("服务层方法:{}--执行时间:{}毫秒"+ joinPoint.getSignature()+ (System.currentTimeMillis() - startTime)));
        return result;
    }

    @Before("GatheringService()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println(("访问时间：{}--访问接口:{}"+ new Date()+ joinPoint.getSignature()));
    }

    //添加缓存
    @Cacheable(value = "gathering",key = "#id")
    public Gathering findById(String id) {
        return gatheringDao.getById(id);
    }

    //删除缓存
    @CacheEvict(value = "gathering",key = "#gathering.id")
    public void updateById(Gathering gathering) {
        gatheringDao.save(gathering);
    }
}
